﻿define(['app'], function (app) {
    app.lazy.controller("BooksController",
        [
            "$scope",
            "$http",
            "$sce",
            "$routeParams",
            "localize",
            function ($scope, $http, $sce, $routeParams, localize) {

                var lastSearchParams = "";
                // defaultPageSize = 20;
                $scope.paging = {
                    PageSize: 20
                };

                var isMod = $routeParams.type && $routeParams.type.toLowerCase() == "mod";
                if (!isMod && $routeParams.type.toLowerCase() != "author") {
                    // TODO: invalid url pattern
                }
        
                var query = function (url) {
                    $http.get(url).success(function (data) {
                        // TODO: access denied (e.g. non-mod user browses the mod list, or unauthenticated access due to session timed out)
                        $scope.books = data.Books;
                        $scope.search = data.Search;
                        $scope.paging = data.Paging;

                        $scope.showPaging = data.Paging && data.Paging.TotalPages && parseInt(data.Paging.TotalPages) > 1;

                        if ($scope.books && $scope.books.length > 0) {
                            angular.forEach($scope.books, function (book) {
                                book.Introduction = $sce.trustAsHtml(book.Introduction);

                                book.selectedCategories = [];
                                angular.forEach(book.Categories, function (categoryId) {
                                    var category = $scope.categories[categoryId];
                                    if (category) {
                                        book.selectedCategories.push(category);
                                        category.selected = true;
                                    }
                                });
                            });
                        }
                    });
                };
                var doSearch = function () {
                    pageChanged(1);
                };
    
                var pageChanged = function (page) {
                    $scope.paging.PageIndex = page - 1;

                    var url = "/dashboard/services/book?";

                    var searchParams = [];

                    if (isMod) {
                        searchParams.push("isMod=true");
                    }

                    if ($scope.search) {
                        angular.forEach($scope.search, function (value, key) {
                            searchParams.push(key + "=" + (value ? encodeURIComponent(value) : ""));
                        });
                    }
        
                    searchParams.push("PageSize=" + $scope.paging.PageSize);
                    searchParams.push("PageIndex=" + $scope.paging.PageIndex);
            
                    if (searchParams.length > 0) {
                        url += searchParams.join("&");
                    }
        
                    query(url);
                };

                $scope.doSearch = doSearch;
                $scope.pageChanged = pageChanged;

                $scope.orderOptions = {
                    "likes": "Order by number of Likes",
                    "views": "Order by number of Views",
                    "name": "Order by name",
                    "id": "Order by date added"
                };

                $scope.orderDirections = {
                    "asc": "Ascending",
                    "desc": "Descending"
                };

                $scope.setOrder = function (val) {
                    $scope.search.OrderBy = val;
                    console.log(val);
                };

                $scope.setOrderDirection = function (val) {
                    $scope.search.OrderDirection = val;
                    console.log(val);
                };

                localize.setCurrentModule("Author_Books");
        
                // category
                $http.get("/dashboard/services/category").success(function (data) {
                    $scope.categories = data;
                    doSearch();
                });
            }
        ]
    );

    app.lazy.filter("getCategoryById", function () {
        return function (key, categories) {
            if (key && categories && categories[key]) return categories[key];
            return "";
        }
    });
});